package com.sylli.oeuf.server.game.logic;

import java.util.Collection;
import java.util.HashSet;
import java.util.Vector;

import com.sylli.oeuf.server.game.logic.invocation.AndInvocationCondition;
import com.sylli.oeuf.server.game.logic.invocation.DispatcherEntry;
import com.sylli.oeuf.server.game.logic.invocation.InvocationCondition;
import com.sylli.oeuf.server.game.logic.invocation.InvocationEntry;
import com.sylli.oeuf.server.game.logic.invocation.InvocationEvent;
import com.sylli.oeuf.server.game.logic.invocation.InvocationParameter;
import com.sylli.oeuf.server.game.logic.skill.NormalAttack;

public class Plan implements DispatcherEntrySource {

	private int planId;
	
	private Skill defaultSkill;
	
	private int defaultSkillRank;
	
	private Vector<DispatcherEntry> planEntries = new Vector<DispatcherEntry>();
	
	public final static Plan defaultPlan;
	
	static {
		defaultPlan = new Plan();
		defaultPlan.defaultSkill = new NormalAttack();
		defaultPlan.defaultSkillRank = 3;
	}
	

	private static Collection<InvocationEvent> availableHandlers = new HashSet<InvocationEvent>();
	
	static {
		availableHandlers.add(InvocationEvent.STD_IE_ON_ATTACK);
	}
	
	@Override
	public Collection<InvocationEvent> getAvailableHandlers() {
		return availableHandlers;
	}

	@Override
	public Collection<DispatcherEntry> getDispatcherEntries(
			InvocationEvent event) {
		if (event.equals(InvocationEvent.STD_IE_ON_ATTACK)) {
			Collection<DispatcherEntry> ret = new HashSet<DispatcherEntry>();
			ret.addAll(planEntries);
			ret.add(getDefaultSkillDispatcherEntry());
			return ret;
		} else {
			return null;
		}
	}
	
	private DispatcherEntry getDefaultSkillDispatcherEntry() {
		DispatcherEntry de = new DispatcherEntry();
		de.setCondition(InvocationCondition.ALWAYS_TRUE_IC);
		InvocationEntry ent = defaultSkill.getInvocationEntryOn(InvocationEvent.STD_IE_ON_ACTIVE);
		de.setAction(ent.getAction());
		de.setParams(new InvocationParameter(defaultSkillRank, 10000, defaultSkill.getDefaultTarget()));
		return de;
	}
	
	public void addInvocationEntry(InvocationCondition planActionCondition, 
			InvocationParameter param, Skill activeSkill) {
		DispatcherEntry de = new DispatcherEntry();
		InvocationEntry en = activeSkill.getInvocationEntryOn(InvocationEvent.STD_IE_ON_ACTIVE);
		de.setAction(en.getAction());
		de.setParams(param);
		de.setCondition(new AndInvocationCondition(planActionCondition, en.getCondition()));
		addDispatcherEntry(de);
	}
	
	public void addDispatcherEntry(DispatcherEntry ie) {
		planEntries.add(ie);
	}
	
	public int getPlanId() {
		return planId;
	}

	public void setPlanId(int planId) {
		this.planId = planId;
	}

	public Skill getDefaultSkill() {
		return defaultSkill;
	}

	public void setDefaultSkill(Skill defaultSkill) {
		this.defaultSkill = defaultSkill;
	}

	public int getDefaultSkillRank() {
		return defaultSkillRank;
	}

	public void setDefaultSkillRank(int defaultSkillRank) {
		this.defaultSkillRank = defaultSkillRank;
	}
}

